package com.szh.mysobproject.ui.activity

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.DownloadManager
import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Environment
import android.view.View
import android.webkit.DownloadListener
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.getSystemService
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import by.kirich1409.viewbindingdelegate.viewBinding
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.PermissionUtils.permission
import com.blankj.utilcode.util.ThreadUtils
import com.gyf.immersionbar.ImmersionBar
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.szh.mysobproject.R
import com.szh.mysobproject.aop.DebugLog
import com.szh.mysobproject.app.AppActivity
import com.szh.mysobproject.databinding.GalleryActivityBinding
import com.szh.mysobproject.http.response.model.WallpaperBean
import com.szh.mysobproject.manager.ThreadPoolManager
import com.szh.mysobproject.other.IntentKey
import com.szh.mysobproject.other.PermissionCallback
import com.szh.mysobproject.ui.adapter.PhotoAdapter
import com.szh.mysobproject.util.*
import com.szh.mysobproject.viewmodel.app.Repository
import com.szh.mysobproject.viewmodel.discover.DiscoverViewModel
import retrofit2.http.Url
import timber.log.Timber
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.*
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.math.pow

/**
 * @author : create by  szh
 * @date : 2022/7/14 14:32
 * @desc :发现图片列表查看页
 */

class GalleryActivity : AppActivity() {

    private val mBinding by viewBinding<GalleryActivityBinding>()
    private val mPhotoAdapter = PhotoAdapter(fillBox = true)
    private val mPhotoList = arrayListOf<WallpaperBean.Res.Vertical>()
    private val mDiscoverViewModel by viewModels<DiscoverViewModel>()
    private var mCurrentPageIndex = 0
    private var isShow = true

    override fun getLayoutId(): Int = R.layout.gallery_activity

    override fun initObserver() {
        val loadMoreModule = mPhotoAdapter.loadMoreModule
        mDiscoverViewModel.verticalPhotoList.observe(this) { verticalPhotoList ->
            Timber.d(verticalPhotoList.toJson())
            loadMoreModule.apply {
                mPhotoAdapter.addData(verticalPhotoList.toList())
                isEnableLoadMore = true
                loadMoreComplete()
            }
        }
    }

    override fun initEvent() {
        mPhotoAdapter.setOnItemLongClickListener { verticalPhoto, _ ->
            //打开指定的一张照片
            Timber.d("imgUrl is ${verticalPhoto.img.trim()}")
            val intent = Intent(Intent.ACTION_VIEW)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(Uri.parse(verticalPhoto.img.trim()), "image/*")
            try {
                startActivity(intent)
            } catch (e: Exception) {
                e.printStackTrace()
                Timber.e(e.message)
            }
        }
        mPhotoAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                isEnableLoadMore = false
                mDiscoverViewModel.loadMorePhotoList()
            }
        }
        mPhotoAdapter.setOnItemClickListener { _, _ ->
            toggleStatus()
        }
        //分享图片
        mBinding.shareTv.setOnClickListener {
            val intent = IntentUtils.getShareImageIntent(getImageUri())
            startActivity(intent)
        }
        mBinding.downLoadPhotoTv.setOnClickListener {
            // 权限框架内部已经做了适配，直接申请 Manifest.permission.MANAGE_EXTERNAL_STORAGE 权限即可
            //下载壁纸
            lifecycleScope.launchWhenCreated {
                val hasPermission =
                    requestXXPermissions(activity, Manifest.permission.MANAGE_EXTERNAL_STORAGE)
                takeIf { hasPermission }?.let {
                    val dm = getSystemService<DownloadManager>() ?: return@let
                    val sourceUri = getImageUri()
                    val verticalPhotoBean = getCurrentVerticalPhotoBean()
                    val request = DownloadManager.Request(sourceUri)
                        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                        .setTitle(verticalPhotoBean.id)
                        .setDescription(verticalPhotoBean.id)
                        .setDestinationInExternalPublicDir(
                            Environment.DIRECTORY_PODCASTS,
                            "阳光沙滩${File.pathSeparator}${verticalPhotoBean.preview}.png"
                        )
                    dm.enqueue(request)
                    simpleToast("已加入下载队列,请查看通知栏")
                }
            }
        }
        //设置壁纸
        val wallpaperManager = WallpaperManager.getInstance(this)
        mBinding.settingWallpaperTv.setOnClickListener {
            toast("开始准备壁纸")
            lifecycleScope.launchWhenCreated {
                val imageFile = DownloadHelper.ofType<File>(this@GalleryActivity, getImageUri())
                val success = if (imageFile == null) {
                    false
                } else {
                    wallpaperManager.setWallpaper(imageFile.inputStream())
                }
                toast(if (success) "壁纸设置成功" else "壁纸设置失败")
            }
        }
    }

    @SuppressLint("MissingPermission")
    private suspend fun WallpaperManager.setWallpaper(inputStream: InputStream) =
        suspendCoroutine<Boolean> {
            ThreadPoolManager.getInstance().execute {
                try {
                    setStream(inputStream)
                    it.resume(true)
                } catch (e: IOException) {
                    e.printStackTrace()
                    it.resume(false)
                }
            }
        }

    private fun toggleStatus() {
        isShow = isShow.not()
        switchUIStatus()
    }

    private fun switchUIStatus() {
        mBinding.toolMenuGroup.isVisible = isShow
    }

    override fun initView() {
        //隐藏状态栏全屏显示壁纸
        ImmersionBar.hideStatusBar(window)
        mBinding.galleryViewPager2.apply {
            orientation = ViewPager2.ORIENTATION_VERTICAL
            adapter = mPhotoAdapter
        }
        mBinding.settingWallpaperTv.setRoundRectBg(Color.parseColor("#66393939"), 8.dp)

    }

    /**
     * 请求权限
     */
    private suspend fun requestXXPermissions(context: Context, permission: String) =
        suspendCoroutine { cont: Continuation<Boolean> ->
            XXPermissions.with(context)
                .permission(permission)
                .request(object : PermissionCallback() {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        cont.resume(true)
                    }

                    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                        super.onDenied(permissions, never)
                        cont.resume(false)
                    }
                })
        }

    private fun getImageUri(): Uri {
        val verticalPhotoBean = getCurrentVerticalPhotoBean()
        Timber.d(verticalPhotoBean.toJson())
        val previewUrl = verticalPhotoBean.preview
        Timber.d("getImageUri：===> previewUrl is $previewUrl")
        return Uri.parse(previewUrl)
    }

    private fun getCurrentVerticalPhotoBean() = mPhotoList[mBinding.galleryViewPager2.currentItem]


    override fun initData() {
        val intent = intent
        val photoId = intent.getStringExtra(IntentKey.ID)
        Timber.d("photoId is $photoId")
        val cacheVerticalPhotoList = Repository.getPhotoList()
        mPhotoList.apply {
            Timber.d("cacheVerticalPhotoList is $cacheVerticalPhotoList")
            addAll(cacheVerticalPhotoList)
        }
        mCurrentPageIndex = mPhotoList.indexOfFirst { photoId == it.id }
        mPhotoAdapter.setList(mPhotoList)
        mBinding.galleryViewPager2.setCurrentItem(mCurrentPageIndex, false)
    }

    companion object {

        private const val SHARE_ELEMENT_NAME = "photo"

        @JvmStatic
        @DebugLog
        fun start(context: Context, id: String) {
            val intent = Intent(context, GalleryActivity::class.java)
            intent.putExtra(IntentKey.ID, id)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }

        @JvmStatic
        @DebugLog
        fun smoothEntry(activity: Activity, id: String, shareElement: View) {
            val aos = ActivityOptionsCompat.makeSceneTransitionAnimation(
                activity,
                shareElement,
                SHARE_ELEMENT_NAME
            )
            val intent = Intent(activity, GalleryActivity::class.java)
            intent.putExtra(IntentKey.ID, id)
            // 请求码必须在 2 的 16 次方以内
            val requestCode = Random().nextInt(2.0.pow(16.0).toInt())
            ActivityCompat.startActivityForResult(activity, intent, requestCode, aos.toBundle())
        }
    }
}